<?php

class modifMedia extends Controller {


	function index()
	{
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->load->helper('url');
		
		$data="";
		
		$query = $this->db->query("SELECT ID_Media,chemin FROM media where type='image' and utilisateur ='".$_COOKIE['login']."'");
		foreach ($query->result_array() as $row) 
			$data["images"][]=array($row['ID_Media'],$row['chemin']);
			
		$query = $this->db->query("SELECT ID_Media,chemin FROM media where type='audio' and utilisateur ='".$_COOKIE['login']."'" );
		foreach ($query->result_array() as $row) 
			$data["audios"][]=array($row['ID_Media'],$row['chemin']);
			
		$query = $this->db->query("SELECT ID_Media,chemin FROM media where type='video' and utilisateur ='".$_COOKIE['login']."'" );
		foreach ($query->result_array() as $row) 
			$data["videos"][]=array($row['ID_Media'],$row['chemin']);
			
			
		if ($this->input->post('ID_Media')!=""){
		
		$query = $this->db->query("SELECT chemin,oeuvre,type FROM media where ID_Media=".$this->input->post('ID_Media'));
		$row = $query->row();
		$data["media"]["id"]=$this->input->post('ID_Media');
		$data["media"]["chemin"]=$row->chemin;
		$data["media"]["oeuvre"]=$row->oeuvre;
		$data["media"]["type"]=$row->type;
		}
		
		if (isset($_POST['modifier'])){
		
			$oeuvre = $this->input->post("oeuvre");
			
		    if($this->input->post('currentOeuvre') != ""){
			
			$query = $this->db->query("SELECT image_principale FROM oeuvre where ID_Oeuvre=".$this->input->post('currentOeuvre'));
			$row = $query->row();
			$image_principale = $row->image_principale;
			
			}
			else $image_principale =0;
			
			if($image_principale != $this->input->post('media')){
			
				if($oeuvre!=0) $this->db->set('oeuvre',$oeuvre);
				else $this->db->set('oeuvre',null);
				$this->db->where('ID_Media',$this->input->post('media'));
				$this->db->update('media'); 
				$data["message"]="Modification prise en compte!";
			}
			
			else $data["message"]="Vous ne pouvez pas d&eacute;associer ce media de son oeuvre car il a &eacute;t&eacute; enregistr&eacute; en tant qu'image principale de l'oeuvre.\nVeuillez aller sur la page de l'oeuvre pour changer ce parametre.\nMerci.";
		  
		}
		
		
		$query = $this->db->query("SELECT ID_Oeuvre,nom FROM oeuvre where utilisateur ='".$_COOKIE['login']."'");
		foreach ($query->result_array() as $row) 
			$data["oeuvres"][]=array($row['ID_Oeuvre'],$row['nom']);
		
		$this->load->view('modifMedia',$data);
	}
	
	
}
?>
